/*
 * Problem:
 * Find a Pythagorean triplet a < b < c for which a^2 + b^2 = c^2,
 * a, b, c - natural numbers.
 * There exists only one Pyth. triplet for which a + b + c = 1000.
 * Find the product abc.
 */
#include <stdio.h>
#include <math.h>

// pow for ints
int powi(int x, int pow)
{
	int i;
	for ( i = 1; i < pow; i++ ) {
		x = x * x;
	}
	return x;
}

int is_right_triangle(int a, int b, int c)
{
	if ( powi(c, 2) == powi(a, 2) + powi(b, 2) ) {
		return 1;
	}
	return 0;
}

/*
 * Loops for all possible a (1-997),
 * then loops for all possible b's for that a (2-998)
 * and checks the requirements.
 */
void loop(int *a, int *b, int *c)
{
	int i, ii;
	int outer_outer_c; // for use in a's loop
	int outer_c; // for use in b's loop

	outer_outer_c = *c;

	for ( i = 1; i <= 997; i++ ) {
		*c = outer_outer_c;
		*a = i;
		*c = *c - *a;
		outer_c = *c;
		for ( ii = *a + 1; ii <= 998; ii++ ) {
			*b = ii;
			if( (*c - *b) <= 0 )
				break;
			*c = outer_c - *b;
			if ( is_right_triangle(*a, *b, *c) ) {
				return;
			}
		}
	}
	printf("Not found\n");
}

int
main(void)
{
	int a, b;
	int c = 1000;

	loop(&a, &b, &c);

	printf("a: %d, b: %d, c: %d \n", a, b, c);
	printf("a * b * c: %d\n", a*b*c);

	return 0;
}
